	 function update_chars_left(max_len, target_input, display_element) {
	  var lines = target_input.val().split('\n');
	  var enterCharacters = lines.length - 1;
	  
	  var text_len = target_input.val().length + enterCharacters;
	   if (text_len >= max_len) {
	       target_input.val(target_input.val().substring(0, max_len - enterCharacters)); // truncate
	       display_element.html('0 remaining');
	   } else {
	       display_element.html(max_len - text_len + " remaining");
	   }
	};





	AJS.$(document).ready(function(){
		AJS.$('#note-edit-form .delete-note-link').live('click', function(){	 		
	 		if (confirm('Are you sure you want to delete this?')) {
	 			AJS.$(this).attr('href', AJS.$(this).attr('href') + '&url=' + AJS.$(location).attr('href'));
	 			return true;
	 		}
	 		return false;
	 	});
	 	AJS.$('#note-edit-form ').live('submit', function(){
	 	// validate fields
			if (AJS.$(this).find('#update-content').val() == '') {
				alert('The content must be not empty!');
				return false;
			}
			// set redirect url
			AJS.$(this).find('[name="url"]').val(AJS.$(location).attr('href'));
	 	});
	 	AJS.$('#memo-edit-form').live('submit', function(){
			AJS.$(this).find('[name="url"]').val(AJS.$(location).attr('href'));
	 	});
	 	
	 	AJS.$('#update-content').live('keyup keydown',function(e) {
       		update_chars_left(255, jQuery(this), jQuery(this).parent().find("#update_remain"));
	 	});

		AJS.log(AJS.version); // "3.0"
		// To use it in all its glory
		if (AJS.version < "3.0") {
			// take advantage of the awesomeness of AUI 3.0
		} else {
			// write awesome custom code
			var dlg_note = new AJS.Dialog({width:600, height:330, id:"note-edit-dialog", closeOnOutsideClick: true});
			
			// PAGE 0 (first page)
			// adds header for first page
			dlg_note.addHeader("Update Note");
			
			// add panel 1
			dlg_note.addPanel("Panel 1", "<p><form id='note-edit-form' action='../plugins/servlet/processservlet' method='get' class='aui unsectioned'></form></p>", "panel-body");
			dlg_note.get("panel:0").setPadding(0);
			
			// Add events to dialog trigger elements
			AJS.$(".note-editable").click(function() {
			    // PREPARE FOR DISPLAY
			    // start first page, first panel
			    dlg_note.gotoPage(0);
			    dlg_note.gotoPanel(0);
				update_chars_left(255, AJS.$(this).find("#update-content"), AJS.$(this).find("#update_remain"));
			    AJS.$('#note-edit-form').html(AJS.$(this).find('.hide_content').html());
				
			    // show dialog
			    dlg_note.show();
			    
			});
			
			// create dialog for memo
			var dlg_memo = new AJS.Dialog({width:400, height:150, id:"memo-edit-dialog", closeOnOutsideClick: true});
			dlg_memo.addHeader("Update Memo");
			
			// add panel 1
			dlg_memo.addPanel("Panel 1", "<p><form id='memo-edit-form' action='../plugins/servlet/processservlet' method='get' class='aui unsectioned'></form></p>", "panel-body");
			dlg_memo.get("panel:0").setPadding(0);
			
			// Add events to memo dialog
			AJS.$(".memo-editable").click(function() {
			    // PREPARE FOR DISPLAY
			    // start first page, first panel
			    dlg_memo.gotoPage(0);
			    dlg_memo.gotoPanel(0);

			    AJS.$('#memo-edit-form').html(AJS.$(this).find('.hide_content').html());

			    // show dialog
			    dlg_memo.show();
			});
		}
	});